package com.example.elmmerchants.Setting;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;

import androidx.appcompat.app.AppCompatActivity;

import com.example.elmmerchants.R;
import com.example.elmmerchants.SQL.Connection_SQL;

import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Set_Check_Licence_Page extends AppCompatActivity {

    EditText set_social_credit_code, set_operator_name, set_company_name, set_company_place, set_registered_capital, set_issing_authority, set_approval_date, set_business_scope;
    Switch set_is_long_term_vai_lidity;
    ImageView business_licence_pic;
    ImageView food_business_licence_pic;
    EditText set_number, set_business_name, set_code, set_representative_person, set_live_where_in_food_licence, set_business_place_in_food_licence, set_sale_what, set_business_scope_in_food_licence, set_validity_date_in_food_licence, set_license_number_in_food_licence, set_supervisory_agency_in_food_licence, set_supervisory_person_in_food_licence, set_report_phone_number_in_food_licence, set_issuer_in_food_licence;
    LinearLayout save;

    String email, social_credit_code_in_licence, operator_name_in_licence, company_name_in_licence, company_place_in_licence, registered_capital_in_licence, issing_authority_in_licence, approval_date_in_licence, business_scope_in_licence;
    int is_long_term_vai_lidity_in_licence;
    byte[] licence_image = new byte[0];
    boolean is_loading_data = false;
    String set_number_in_food_licence, set_business_name_in_food_licence, set_code_in_food_licence, set_representative_person_in_food_licence, set_live_where_in_food_licence_in_food_licence, set_business_place_in_food_licence_in_food_licence, set_sale_what_in_food_licence, set_business_scope_in_food_licence_text, set_validity_date_in_food_licence_text, set_license_number_in_food_licence_text, set_supervisory_agency_in_food_licence_text, set_supervisory_person_in_food_licence_text, set_report_phone_number_in_food_licence_text, set_issuer_in_food_licence_text;
    byte[] food_licence_image = new byte[0];

    private class Loading_Licence_Image_Thread extends Thread {
        @Override
        public void run() {
            is_loading_data = false;

            ExecutorService executorService = Executors.newSingleThreadExecutor();
            executorService.execute(() -> {
                try {
                    Connection_SQL userSQL = new Connection_SQL();
                    Connection connection = userSQL.connection();
                    String sql = "SELECT * FROM merchant WHERE email = ?;";
                    PreparedStatement preparedStatement = connection.prepareStatement(sql);
                    preparedStatement.setString(1, email);
                    ResultSet set = preparedStatement.executeQuery();
                    while (set.next()) {
                        licence_image = set.getBytes("licence_picture");
                        food_licence_image = set.getBytes("food_business_licence_picture");
                    }
                    is_loading_data = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            while (!is_loading_data) {
                try {
                    Thread.sleep(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Bitmap decodedImage = BitmapFactory.decodeByteArray(licence_image, 0, licence_image.length);
                    business_licence_pic.setImageBitmap(decodedImage);

                    Bitmap decodedImage1 = BitmapFactory.decodeByteArray(food_licence_image, 0, food_licence_image.length);
                    food_business_licence_pic.setImageBitmap(decodedImage1);
                }
            });
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_check_licence_page);

        set_number_in_food_licence = getIntent().getStringExtra("set_number_in_food_licence");
        set_business_name_in_food_licence = getIntent().getStringExtra("set_business_name_in_food_licence");
        set_code_in_food_licence = getIntent().getStringExtra("set_code_in_food_licence");
        set_representative_person_in_food_licence = getIntent().getStringExtra("set_representative_person_in_food_licence");
        set_live_where_in_food_licence_in_food_licence = getIntent().getStringExtra("set_live_where_in_food_licence_in_food_licence");
        set_business_place_in_food_licence_in_food_licence = getIntent().getStringExtra("set_business_place_in_food_licence_in_food_licence");
        set_sale_what_in_food_licence = getIntent().getStringExtra("set_sale_what_in_food_licence");
        set_business_scope_in_food_licence_text = getIntent().getStringExtra("set_business_scope_in_food_licence_text");
        set_validity_date_in_food_licence_text = getIntent().getStringExtra("set_validity_date_in_food_licence_text");
        set_license_number_in_food_licence_text = getIntent().getStringExtra("set_license_number_in_food_licence_text");
        set_supervisory_agency_in_food_licence_text = getIntent().getStringExtra("set_supervisory_agency_in_food_licence_text");
        set_supervisory_person_in_food_licence_text = getIntent().getStringExtra("set_supervisory_person_in_food_licence_text");
        set_report_phone_number_in_food_licence_text = getIntent().getStringExtra("set_report_phone_number_in_food_licence_text");
        set_issuer_in_food_licence_text = getIntent().getStringExtra("set_issuer_in_food_licence_text");
        email = getIntent().getStringExtra("email");
        social_credit_code_in_licence = getIntent().getStringExtra("social_credit_code_in_licence");
        operator_name_in_licence = getIntent().getStringExtra("operator_name_in_licence");
        company_name_in_licence = getIntent().getStringExtra("company_name_in_licence");
        company_place_in_licence = getIntent().getStringExtra("company_place_in_licence");
        registered_capital_in_licence = getIntent().getStringExtra("registered_capital_in_licence");
        issing_authority_in_licence = getIntent().getStringExtra("issing_authority_in_licence");
        approval_date_in_licence = getIntent().getStringExtra("approval_date_in_licence");
        business_scope_in_licence = getIntent().getStringExtra("business_scope_in_licence");
        is_long_term_vai_lidity_in_licence = getIntent().getIntExtra("is_long_term_vai_lidity_in_licence", 0);

        set_social_credit_code = findViewById(R.id.set_social_credit_code);
        business_licence_pic = findViewById(R.id.business_licence_pic);
        set_company_name = findViewById(R.id.set_company_name);
        set_operator_name = findViewById(R.id.set_operator_name);
        set_company_place = findViewById(R.id.set_company_place);
        set_registered_capital = findViewById(R.id.set_registered_capital);
        set_issing_authority = findViewById(R.id.set_issing_authority);
        set_is_long_term_vai_lidity = findViewById(R.id.set_is_long_term_vai_lidity);
        set_approval_date = findViewById(R.id.set_approval_date);
        set_business_scope = findViewById(R.id.set_business_scope);
        set_number = findViewById(R.id.set_number);
        set_business_name = findViewById(R.id.set_business_name);
        set_code = findViewById(R.id.set_code);
        set_representative_person = findViewById(R.id.set_representative_person);
        set_live_where_in_food_licence = findViewById(R.id.set_live_where_in_food_licence);
        set_business_place_in_food_licence = findViewById(R.id.set_business_place_in_food_licence);
        set_sale_what = findViewById(R.id.set_sale_what);
        set_business_scope_in_food_licence = findViewById(R.id.set_business_scope_in_food_licence);
        set_validity_date_in_food_licence = findViewById(R.id.set_validity_date_in_food_licence);
        set_license_number_in_food_licence = findViewById(R.id.set_license_number_in_food_licence);
        set_supervisory_agency_in_food_licence = findViewById(R.id.set_supervisory_agency_in_food_licence);
        set_supervisory_person_in_food_licence = findViewById(R.id.set_supervisory_person_in_food_licence);
        set_report_phone_number_in_food_licence = findViewById(R.id.set_report_phone_number_in_food_licence);
        set_issuer_in_food_licence = findViewById(R.id.set_issuer_in_food_licence);
        food_business_licence_pic = findViewById(R.id.food_business_licence_pic);
        save = findViewById(R.id.save);

        Loading_Licence_Image_Thread loadingLicenceImageThread = new Loading_Licence_Image_Thread();
        loadingLicenceImageThread.start();

        if (!social_credit_code_in_licence.isEmpty()) {
            set_social_credit_code.setText(social_credit_code_in_licence);
            set_social_credit_code.setEnabled(false);
        }

        if (!operator_name_in_licence.isEmpty()) {
            set_operator_name.setText(operator_name_in_licence);
            set_operator_name.setEnabled(false);
        }

        if (!company_name_in_licence.isEmpty()) {
            set_company_name.setText(company_name_in_licence);
            set_company_name.setEnabled(false);
        }

        if (!company_place_in_licence.isEmpty()) {
            set_company_place.setText(company_place_in_licence);
            set_company_place.setEnabled(false);
        }

        if (!registered_capital_in_licence.equals("无")) {
            set_registered_capital.setText(registered_capital_in_licence);
        }

        if (!issing_authority_in_licence.isEmpty()) {
            set_issing_authority.setText(issing_authority_in_licence);
            set_issing_authority.setEnabled(false);
        }

        if (is_long_term_vai_lidity_in_licence == 1) {
            set_is_long_term_vai_lidity.setChecked(true);
        }

        if (!approval_date_in_licence.isEmpty()) {
            set_approval_date.setText(approval_date_in_licence);
            set_approval_date.setEnabled(false);
        }

        if (!business_scope_in_licence.isEmpty()) {
            set_business_scope.setText(business_scope_in_licence);
        }

        if (!set_number_in_food_licence.isEmpty()) {
            set_number.setText(set_number_in_food_licence);
        }

        if (!set_business_name_in_food_licence.isEmpty()) {
            set_business_name.setText(set_business_name_in_food_licence);
            set_business_name.setEnabled(false);
        }

        if (!set_code_in_food_licence.isEmpty()) {
            set_code.setText(set_code_in_food_licence);
            set_code.setEnabled(false);
        }

        if (!set_representative_person_in_food_licence.isEmpty()) {
            set_representative_person.setText(set_representative_person_in_food_licence);
            set_representative_person.setEnabled(false);
        }

        if (!set_live_where_in_food_licence_in_food_licence.isEmpty()) {
            set_live_where_in_food_licence.setText(set_live_where_in_food_licence_in_food_licence);
        }

        if (!set_business_place_in_food_licence_in_food_licence.isEmpty()) {
            set_business_place_in_food_licence.setText(set_business_place_in_food_licence_in_food_licence);
            set_business_place_in_food_licence.setEnabled(false);
        }

        if (!set_sale_what_in_food_licence.isEmpty()) {
            set_sale_what.setText(set_sale_what_in_food_licence);
            set_sale_what.setEnabled(false);
        }

        if (!set_business_scope_in_food_licence_text.isEmpty()) {
            set_business_scope_in_food_licence.setText(set_business_scope_in_food_licence_text);
            set_business_scope_in_food_licence.setEnabled(false);
        }

        if (!set_validity_date_in_food_licence_text.isEmpty()) {
            set_validity_date_in_food_licence.setText(set_validity_date_in_food_licence_text);
            set_validity_date_in_food_licence.setEnabled(false);
        }

        if (!set_license_number_in_food_licence_text.isEmpty()) {
            set_license_number_in_food_licence.setText(set_license_number_in_food_licence_text);
            set_license_number_in_food_licence.setEnabled(false);
        }

        if (!set_supervisory_agency_in_food_licence_text.isEmpty()) {
            set_supervisory_agency_in_food_licence.setText(set_supervisory_agency_in_food_licence_text);
        }

        if (!set_supervisory_person_in_food_licence_text.isEmpty()) {
            set_supervisory_person_in_food_licence.setText(set_supervisory_person_in_food_licence_text);
        }

        if (!set_report_phone_number_in_food_licence_text.isEmpty()) {
            set_report_phone_number_in_food_licence.setText(set_report_phone_number_in_food_licence_text);
        }

        if (!set_issuer_in_food_licence_text.isEmpty()) {
            set_issuer_in_food_licence.setText(set_issuer_in_food_licence_text);
        }

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}